package com.example.foobar.controller;

import com.example.foobar.business.service.ImsPzGuanhuaiTrackService;
import com.example.foobar.business.service.impl.DaNewCommerCountServiceImpl;
import com.example.foobar.dto.DABaseDTO;
import com.example.foobar.entity.DaDataAnalysis;
import com.example.foobar.entity.ImsPzGuanhuaiTrack;
import com.example.foobar.dao.service.ImsPzGuanhuaiTrackDaoService;
import lombok.extern.slf4j.Slf4j;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  控制器
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@RestController
@RequestMapping("/imsPzGuanhuaiTrack")
@Module("imsPzGuanhuaiTrackController")
@Api(value = "用户操作记录", tags = {"源数据"})
public class ImsPzGuanhuaiTrackController extends BaseController {

    @Autowired
    private ImsPzGuanhuaiTrackService imsPzGuanhuaiTrackService;

    /**
     * 添加
     */
    @PostMapping("/add")
    @OperationLog(name = "添加", type = OperationLogType.ADD)
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> addImsPzGuanhuaiTrack(@Validated(Add.class) @RequestBody ImsPzGuanhuaiTrack imsPzGuanhuaiTrack) throws Exception {
        boolean flag = imsPzGuanhuaiTrackService.saveImsPzGuanhuaiTrack(imsPzGuanhuaiTrack);
        return ApiResult.result(flag);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @OperationLog(name = "修改", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改", response = ApiResult.class)
    public ApiResult<Boolean> updateImsPzGuanhuaiTrack(@Validated(Update.class) @RequestBody ImsPzGuanhuaiTrack imsPzGuanhuaiTrack) throws Exception {
        boolean flag = imsPzGuanhuaiTrackService.updateImsPzGuanhuaiTrack(imsPzGuanhuaiTrack);
        return ApiResult.result(flag);
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除", response = ApiResult.class)
    public ApiResult<Boolean> deleteImsPzGuanhuaiTrack(@PathVariable("id") Long id) throws Exception {
        boolean flag = imsPzGuanhuaiTrackService.deleteImsPzGuanhuaiTrack(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "详情", type = OperationLogType.INFO)
    @ApiOperation(value = "详情", response = ImsPzGuanhuaiTrack.class)
    public ApiResult<ImsPzGuanhuaiTrack> getImsPzGuanhuaiTrack(@PathVariable("id") Long id) throws Exception {
        ImsPzGuanhuaiTrack imsPzGuanhuaiTrack = imsPzGuanhuaiTrackService.getById(id);
        return ApiResult.ok(imsPzGuanhuaiTrack);
    }

    @GetMapping("/getAnalysis")
    @OperationLog(name = "获取活跃用户数据", type = OperationLogType.INFO)
    @ApiOperation(value = "获取活跃用户数据", response = DaDataAnalysis.class)
    public ApiResult<List<DABaseDTO>> getDaDataAnalysis(String uniacid, Date baseDate, Date startDate, Date endDate){
        List<String> messageTypeList = new ArrayList<>();
        messageTypeList.add(DaNewCommerCountServiceImpl.MESSAGE_TYPE_EVENT);
        messageTypeList.add(DaNewCommerCountServiceImpl.MESSAGE_TYPE_TEXT);
        List<DABaseDTO> activeDataByOfficialAccountIdAndDataRange = imsPzGuanhuaiTrackService.getActiveDataByOfficialAccountIdAndDataRange(uniacid, baseDate, startDate, endDate, messageTypeList);
        return  ApiResult.ok(activeDataByOfficialAccountIdAndDataRange);
    }
}

